package com.nbrc.jsonmodel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.activiti.engine.HistoryService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.task.Task;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import com.nbrc.helper.DateSerializer;

public class MyTask {
	private String id;
	private String name;
	private String businessKey;
	@JsonSerialize(using=DateSerializer.class)
	private Date startTime;
	@JsonSerialize(using=DateSerializer.class)
	private Date dueDate;
	private String processInstanceId;
	
	public String getProcessInstanceId() {
		return processInstanceId;
	}
	public void setProcessInstanceId(String processInstanceId) {
		this.processInstanceId = processInstanceId;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	public Date getDueDate() {
		return dueDate;
	}
	public void setDueDate(Date dueDate) {
		this.dueDate = dueDate;
	}
	
	public String getBusinessKey() {
		return businessKey;
	}
	public void setBusinessKey(String businessKey) {
		this.businessKey = businessKey;
	}
	public Date getStartTime() {
		return startTime;
	}
	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}
	public static MyTask toTask(HistoryService historyService, Task list) {
		// TODO Auto-generated method stub
		MyTask mytask = new MyTask();
		mytask.setStartTime(list.getCreateTime());
		mytask.setDueDate(list.getDueDate());
		mytask.setId(list.getId());
		mytask.setProcessInstanceId(list.getProcessInstanceId());
		mytask.setName(list.getName());
		HistoricProcessInstance hpi = historyService.createHistoricProcessInstanceQuery().processInstanceId(list.getProcessInstanceId()).singleResult();
		mytask.setBusinessKey(hpi.getBusinessKey());
		return mytask;
	}
	public static List<MyTask> toTask(HistoryService historyService, List<Task> tasks) {
		// TODO Auto-generated method stub
		List<MyTask> mytasklist = new ArrayList<>();
		for(Task t: tasks){
			mytasklist.add(MyTask.toTask(historyService,t));
		}
		return mytasklist;
	}
	
}
